package com.wifi.view;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wifi.BaseApplication;
import com.wifi.bean.MacBean;
import com.wifi.eshipewifi.R;

import java.util.ArrayList;


public class CacheConnetAdapter extends BaseAdapter{

    private Context context;
    String[] data;
    BaseApplication application;
    int select_position=-1;
    String ssid;
    boolean isIng=false;
    String[] item_data;
    ArrayList<MacBean> macBeanArrayList=new ArrayList<MacBean>();
    public CacheConnetAdapter(Context context){
        this.context=context;
    }

    public CacheConnetAdapter(Context context, String[] data, BaseApplication application){
        this.context=context;
        this.data=data;
        this.application=application;
        //$scan_savedWifi;账号,密码;

        for (int i=1;i<data.length;i=i+1){
            MacBean macBean=new MacBean();
            item_data = data[i].split(",");
            macBean.setMac(item_data[0]);
            macBean.setMacName(item_data[1]);
            macBeanArrayList.add(macBean);
        }
    }

    public void setCheck(String ssid){
        this.ssid=ssid;
        this.notifyDataSetChanged();
    }


    @Override
    public int getCount(){
        return (data.length-1);
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView = View.inflate(context, R.layout.item_connet, null);
            holder=new ViewHolder();
            holder.tv_item_name = (TextView) convertView.findViewById(R.id.tv_item_name);
            holder.tv_item_time = (TextView) convertView.findViewById(R.id.tv_item_time);
            holder.rl_item = (RelativeLayout) convertView.findViewById(R.id.rl_item);
            holder.tv_item_black = (TextView) convertView.findViewById(R.id.tv_item_black);
            holder.iv_item_icon = (ImageView) convertView.findViewById(R.id.iv_item_icon);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        //$scan_savedWifi;账号,密码;
//        holder.tv_item_time.setVisibility(View.GONE);
        holder.tv_item_black.setText("删除");
        holder.tv_item_name.setText("账号 : "+macBeanArrayList.get(position).getMac());
        holder.tv_item_time.setText("密码 : "+macBeanArrayList.get(position).getMacName());
        holder.tv_item_black.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view = View.inflate(context, R.layout.item_cache_dialog_text, null);
                AlertDialog dialog= new AlertDialog.Builder(context)
                        .setTitle("缓存")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setView(view)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
//                                $delete_savedWifi;账号,密码;
                                application.sendMsg("$delete_savedWifi;"+macBeanArrayList.get(position).getMac()+","+macBeanArrayList.get(position).getMacName()+";");
                                Toast.makeText(context,"已成功删除"+macBeanArrayList.get(position).getMacName()+"，请稍等……",Toast.LENGTH_LONG).show();
//                                    delete_data =data.replace(item_data[position] + "," + item_data[position + 1]+",", "");
                                macBeanArrayList.remove(position);
                                dialog.cancel();
//                                setData();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        }).create();
                dialog .show();

            }
        });

        return convertView;
    }

    public void setData(){
//        item_data= (delete_data.split(","));
        if(macBeanArrayList.size()>0){
            this.notifyDataSetChanged();//todo
        }

    }

    private class ViewHolder{

        private TextView tv_item_name;
        private TextView tv_item_time;
        private RelativeLayout rl_item;
        private TextView tv_item_black;
        private ImageView iv_item_icon;
    }
}
